package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.SysProcurementdeclare;
import com.ruoyi.system.domain.SysPurchaseplan;
import com.ruoyi.system.service.ISysProcurementdeclareService;
import com.ruoyi.system.service.ISysPurchaseplanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDeclaregoods;
import com.ruoyi.system.service.ISysDeclaregoodsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;

/**
 * 采购申报货物Controller
 * 
 * @author ruoyi
 * @date 2020-12-28
 */
@Controller
@RequestMapping("/system/declaregoods")
public class SysDeclaregoodsController extends BaseController
{
    private String prefix = "system/declaregoods";

    @Autowired
    private ISysDeclaregoodsService sysDeclaregoodsService;
    @Autowired
    private ISysProcurementdeclareService sysProcurementdeclareService;
    @Autowired
    private ISysPurchaseplanService sysPurchaseplanService;

    @RequiresPermissions("system:declaregoods:view")
    @GetMapping()
    public String declaregoods()
    {
        return prefix + "/declaregoods";
    }

    /**
     * 查询采购申报货物列表
     */
    @RequiresPermissions("system:declaregoods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDeclaregoods sysDeclaregoods)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        if("10".equals(sysUser.getUserType())){
            sysDeclaregoods.setAttribite3(sysUser.getCompanyId()+"");//采购单位Id
        }
        startPage();
        List<SysDeclaregoods> list = sysDeclaregoodsService.selectSysDeclaregoodsList(sysDeclaregoods);
        return getDataTable(list);
    }

    /**
     * 导出采购申报货物列表
     */
    @RequiresPermissions("system:declaregoods:export")
    @Log(title = "采购申报货物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDeclaregoods sysDeclaregoods)
    {
        List<SysDeclaregoods> list = sysDeclaregoodsService.selectSysDeclaregoodsList(sysDeclaregoods);
        ExcelUtil<SysDeclaregoods> util = new ExcelUtil<SysDeclaregoods>(SysDeclaregoods.class);
        return util.exportExcel(list, "declaregoods");
    }

    /**
     * 新增采购申报货物
     */
    @GetMapping("/add/{procurementDeclareId}")
    public String add(@PathVariable("procurementDeclareId") Long procurementDeclareId, ModelMap mmap)
    {
        SysProcurementdeclare sysProcurementdeclare = sysProcurementdeclareService.selectSysProcurementdeclareById(procurementDeclareId);
        mmap.put("projectName",sysProcurementdeclare.getProjectName());
        mmap.put("procurementDeclareId",sysProcurementdeclare.getProcurementDeclareId());

        return prefix + "/add";
    }

    /**
     * 新增保存采购申报货物
     */
    @RequiresPermissions("system:declaregoods:add")
    @Log(title = "采购申报货物", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDeclaregoods sysDeclaregoods)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        sysDeclaregoods.setAttribite3(sysUser.getCompanyId()+"");//采购单位Id

        return toAjax(sysDeclaregoodsService.insertSysDeclaregoods(sysDeclaregoods));
    }

    /**
     * 修改采购申报货物
     */
    @GetMapping("/edit/{declareGoodsId}")
    public String edit(@PathVariable("declareGoodsId") Long declareGoodsId, ModelMap mmap)
    {
        SysDeclaregoods sysDeclaregoods = sysDeclaregoodsService.selectSysDeclaregoodsById(declareGoodsId);
        SysProcurementdeclare sysProcurementdeclare = sysProcurementdeclareService.selectSysProcurementdeclareById(sysDeclaregoods.getProcurementDeclareId());
        sysDeclaregoods.setAttribite2(sysProcurementdeclare.getProjectName());
        mmap.put("sysDeclaregoods", sysDeclaregoods);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购申报货物
     */
    @RequiresPermissions("system:declaregoods:edit")
    @Log(title = "采购申报货物", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDeclaregoods sysDeclaregoods)
    {

        return toAjax(sysDeclaregoodsService.updateSysDeclaregoods(sysDeclaregoods));
    }

    /**
     * 删除采购申报货物
     */
    @RequiresPermissions("system:declaregoods:remove")
    @Log(title = "采购申报货物", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysDeclaregoodsService.deleteSysDeclaregoodsByIds(ids));
    }
    /**
     * 删除采购申报货物
     */

    @RequestMapping ( value="goodserror")
    @ResponseBody
    public AjaxResult goodsError()
    {
        return AjaxResult.error("请先添加申报表商品后再提交审批！");
    }
    /**
     * 采购申报表提交审批
     * @param procurementDeclareId
     * @param mmap
     * @return
     */
    @RequiresPermissions("system:declaregoods:shenpi")
    @GetMapping("/shenpi/{procurementDeclareId}")
    public String shenpi(@PathVariable("procurementDeclareId") Long procurementDeclareId, ModelMap mmap, HttpServletRequest request, HttpServletResponse response)
    {
        SysProcurementdeclare sysProcurementdeclare = sysProcurementdeclareService.selectSysProcurementdeclareById(procurementDeclareId);
        SysDeclaregoods sysDeclaregoods = new SysDeclaregoods();
        sysDeclaregoods.setProcurementDeclareId(procurementDeclareId);
        List<SysDeclaregoods> sysDeclaregoodsList = sysDeclaregoodsService.selectSysDeclaregoodsList(sysDeclaregoods);
        if(sysDeclaregoodsList.size()>0){
        //项目编号
            Calendar calendar1 = Calendar.getInstance();
            String year = String.valueOf(calendar1.get(Calendar.YEAR));
            String projectNumber="";
            SysProcurementdeclare sysProcurementdeclare2 = sysProcurementdeclareService.findProjectNumber(year);
            if(sysProcurementdeclare2 != null){
                String[] number = sysProcurementdeclare2.getProjectNumber().split("-");
                projectNumber = year + "-" + (Integer.parseInt(number[1]) + 1);
            }else{
                projectNumber = year + "-1";
            }
        //修改项目编号结束
            List<SysProcurementdeclare>sysProcurementdeclareList = sysProcurementdeclareService.selectSysProcurementdeclareList(null);
            String nums = "";
            for (SysProcurementdeclare pro : sysProcurementdeclareList) {
                nums = nums + pro.getProjectNumber() + ",";
            }
            for (int i = 0; i < sysProcurementdeclareList.size(); i++) {
                if (!nums.contains(year + "-" + (i + 1))) {
                    projectNumber = year + "-" + (i + 1);
                    break;
                }
            }
            sysProcurementdeclare.setProjectNumber(projectNumber);
            sysProcurementdeclare.setAttribite3("1");//1表示申报表已经提交状态
            sysProcurementdeclareService.updateSysProcurementdeclare(sysProcurementdeclare);
            SysPurchaseplan sysPurchaseplan = sysPurchaseplanService.selectSysPurchaseplanById(sysProcurementdeclare.getPurchasePlanId());
            sysPurchaseplan.setAttribute3("1");//计划已完成
            mmap.put("procurementDeclare", sysProcurementdeclare);
            return  "system/approve/confirmSubmitApprove";
        }else{
//            redirect(prefix+"/goodsError")
            // 如果是Ajax请求，返回Json字符串。

                return ServletUtils.renderString(response, "{\"msg\":\"请先添加申报表商品后再提交审批！\"}");


        }

    }


}
